<template>
	<view v-if="update" class="shop-car">
		<commonCar :list="goodsProducts" @delect="delect" @accounts="accounts"
               @clickItem="goodsItem">
			<!-- <text>123456</text> -->
		</commonCar>
<!--		<image src="" mode=""></image>-->
	</view>
</template>

<script>
	import commonCar from '@/components/shopCar/shopCar.vue';
	import {ShoppingCartService} from "../../../api/buyer/ShoppingCart/index";
  import {attachmentService} from "../../../api/buyer/public/attachmentService";
  import {BASE_IMG_URL} from "../../../api/service";
  import {NetWork} from "../../../api/buyer/common";
	export default {
		data() {
			return {
				goodsProducts: [
				    // {
				// 	name: '华为旗舰店',
				// 	checked: 1,
				// 	goods: [{
				// 		img: 'http://124.223.178.102:8081/api/img/1/20220518/yi20220518161254291.jpg',
				// 		title: '华为荣耀',
				// 		remark: '256G',
				// 		price: '128.80',
				// 		number: 1,
				// 		checked: 1
				// 	}, {
				// 		img: 'http://124.223.178.102:8081/api/img/1/20220518/yi20220518161254291.jpg',
				// 		title: 'mate40',
				// 		remark: '128G',
				// 		price: '116.80',
				// 		number: 1,
				// 		checked: 1
				// 	}]
				// },
        //   {
				// 	name: '华为旗舰店',
				// 	checked: 1,
				// 	goods: [{
				// 		img: 'http://124.223.178.102:8081/api/img/1/20220518/yi20220518161254291.jpg',
				// 		title: 'P40',
				// 		remark: '64G',
				// 		price: '129.80',
				// 		number: 1,
				// 		checked: 1
				// 	}, {
				// 		img: 'http://124.223.178.102:8081/api/img/1/20220518/yi20220518161254291.jpg',
				// 		title: 'P50',
				// 		remark: '32G',
				// 		price: '156.80',
				// 		number: 1,
				// 		checked: 1
				// 	}]
				// }
				],
        update:true,
			}
		},
		components: {
			commonCar
		},
    created(){
      console.log(NetWork.checkIsLogin())
		  if(NetWork.checkIsLogin()){
        this.init()
      }
    },
    onPullDownRefresh() {
      this.init();
      setTimeout(function () {
        uni.stopPullDownRefresh();
        this.update=false
        this.update=true
      }, 1000);
    },
		methods: {
		  init(){
        ShoppingCartService.findUserShoppingCarts({currentPage:1,pageSize:10}).then(res=>{
          console.log("res",res.data.datas)
          this.goodsProducts=res.data.datas
          this.loadImage()
        })
      },
      loadImage(){
		    this.goodsProducts.forEach(item=>{
          console.log("i",item)
		      item.goods.forEach(good=>{
            console.log(",",good)
            if (good.eid) {
              attachmentService.findFile(good.eid, 'Commodity').then((res) => {
                good.img = BASE_IMG_URL + res.data[0].attachmentAddr
                console.log(good.img)
              })
            }
          })
        })
      },
			delect(e){
				console.log(e);
				let shoppingCartIds=[];
        e.forEach(item=>{
          shoppingCartIds.push(item.shippingCarId);
        })
        // console.log(shoppingCartIds)
				ShoppingCartService.delShoppingCart(shoppingCartIds).then((res) => {
				  if(res.data.code==1) {
            uni.showToast({
              title: "删除失败啦！",
              icon:'exception',
              duration:1000
            });
          }else{
            uni.showToast({
              title: "删除成功",
              icon: 'success',
              duration: 1000
            });
            this.init()
          }
        }).catch((err) => {
          uni.showToast({
            title: "删除失败啦！",
            icon:'exception',
            duration:1000
          });
        })
			},
			accounts(e){
		    if(!e||e.length==0){
          uni.showToast({
            title: "请选中一个结算的商品！",
            icon:'exception',
            duration:1000
          });
		      return
        }
		    if(e.length>1){
          uni.showToast({
            title: "不好意思！暂时不支持一次结算多个商品，请分次结算",
            icon:'none',
            duration:1000
          });
          return
        }
		    let s=e[0];
		    let good={
		      eid:s.eid,
          price:s.price,
          description:s.title,
          specification:s.remark,
          shopId:s.shopId,
          count:s.num,
          url:s.img,
          shoppingCarId:s.shippingCarId
        }
				console.log(good);
        uni.navigateTo({
          url: '/pages/buyer/main/OrderFrom?info=' + encodeURIComponent(JSON.stringify(good)),
          // url: '/pages/staff/application/production/taskList/task-detail'
        })
			},
      goodsItem(item){
        console.log(item)
        let good={
          eid:item.eid,
          price:item.price,
          description:item.title
        }
        console.log(good)
        uni.navigateTo({
          url: '/pages/buyer/main/goodDetailed?good=' + encodeURIComponent(JSON.stringify(good)),
          // url: '/pages/staff/application/production/taskList/task-detail'
        })
      },
		}
	}
</script>

<style lang="scss" scoped>
	.shop-car {
		width: 750rpx;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		background: #F5F5F5;
	}
</style>
